import asyncio
import os.path
from typing import Union

from typing_extensions import Final

from env_config import settings

from asyncio import Task
from concurrent.futures import Future

loop = asyncio.get_event_loop()


class UrlUtil:
    domain: Final[str] = settings.domain
    upload_prefix: Final[str] = settings.upload_prefix

    @classmethod
    def to_absolute_sync(cls, url: Union[str, list, None], engine='') -> Union[str, list, None]:
        if not url:
            return url
        if not engine:
            engine = settings.storage_map_cache.get('default')
        if isinstance(url, list):
            return [UrlUtil.to_absolute_url_sync(val, engine) for val in url]
        elif isinstance(url, str) and ',' in url and not url.startswith('data:image/'):
            urls = [UrlUtil.to_absolute_url_sync(val, engine) for val in url.split(',')]
            return urls
        return UrlUtil.to_absolute_url_sync(url, engine)

    @classmethod
    def to_absolute_url_sync(cls, url: str, engine='') -> str:
        """
        转绝对路径
        转前: /uploads/11.png
        转后: https://127.0.0.1/uploads/11.png
        :param url: 相对路径
        :param engine: 存储引擎
        :return:
        """
        if not url:
            # 此处不能直接返回''，因为[]也会进来，正常应该是进来什么类型，出去要是什么类型
            return url
        if (url.startswith('http') or url.startswith('ftp://')
                or url.startswith('data:image/') or url.startswith('/static/')):
            return url
        if url.find('/') != 0:
            url = '/' + url
        if url.startswith('/routers/static/'):
            return cls.domain + url
        if not engine:
            engine = settings.storage_map_cache.get('default')
        if engine == 'local':
            return cls.domain + cls.upload_prefix + url
        config = settings.storage_map_cache.get(engine)
        return config.get('domain') + url

    @classmethod
    def to_relative_sync(cls, url: Union[str, list, None], engine='local') -> Union[str, list, None]:
        if not url:
            # 此处url为[]时也会为true
            return ''
        if not engine:
            engine = settings.storage_map_cache.get('default')
        if isinstance(url, list):
            urls = [UrlUtil.to_relative_url_sync(val, engine) for val in url]
            return ','.join(urls)
        elif isinstance(url, str) and ',' in url:
            urls = [UrlUtil.to_relative_url_sync(val, engine) for val in url.split(',')]
            return ','.join(urls)
        return UrlUtil.to_relative_url_sync(url, engine)

    @classmethod
    def to_relative_url_sync(cls, url: str, engine='local') -> str:
        """
        转相对路径
        转前: https://127.0.0.1/uploads/11.png
        转后: /uploads/11.png
        :param url:
        :param engine:
        :return:
        """
        if not url or not url.startswith('http') or url.__contains__('/job/'):
            # 排除非http开关和jenkins中的路径不进行转换
            return url
        if not engine:
            engine = settings.storage_map_cache.get('default')
        if engine == 'local':
            return url.replace(settings.domain, '').replace(os.path.join('/', cls.upload_prefix) + '/', '/')
        config = settings.storage_map_cache.get(engine)
        return url.replace(config.get('domain', ''), '').replace(os.path.join('/', cls.upload_prefix) + '/', '')


